///////////////////////////////////////////////////////// //Programa para controlar la lampara rafab. //Outputs: // - Servomotor (abre/cierra el paso de la luz) // - 30 x WS2812B 5050 Addressable SMD smart rgb led //Inputs: // - Light sensor PT15-21C // - Bluetooth App ///////////////////////////////////////////////////////// ///////////// //LIBRARIES// ///////////// //Bluetooth library #include "BluetoothSerial.h" #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif BluetoothSerial SerialBT; //Leds library #include #ifdef __AVR__ #include // Required for 16 MHz Adafruit Trinket #endif //////// //PINS// //////// #define LedPin 15 //Pin for leds control #define ServoPin 16 //Pin for servomotor control #define LSensorPin 34// Pin for light sensor PT15-21C ///////////// //VARIABLES// ///////////// //Leds control #define NUMPIXELS 30 // NÂșLeds int BRIGHTNESS=255; int i_red=254; //save value to red for RGB int i_blue=254;// save value to blue for RGB int i_green=254;// save value to green for RGB //Setting up the NeoPixel library fot leds Adafruit_NeoPixel pixels(NUMPIXELS, LedPin, NEO_GRB + NEO_KHZ800); //Servomotor control int dutyCycle = 0;//de 6 a 33 para el servo /* Setting PWM properties */ const int PWMFreq = 50; const int PWMChannel = 0; const int PWMResolution = 8; //const int MAX_DUTY_CYCLE = (int)(pow(2, PWMResolution) - 1); bool b_autoServo=true; int servoMin=6;//6 int servoMax=12;//12 //Light sensor control int LSensorValue = 0; //Light sensor value 0 to 4095 int lsvMin=0; int lsvMax=1000; //light sensor value for open servo //Bluetooth serial String recv = ""; int MyP = 0; int MyI = 0; String subs[10]; int ia=0; void setup() { Serial.begin(115200); pinMode(ServoPin, OUTPUT); pinMode(LedPin, OUTPUT); pinMode(LSensorPin, INPUT); SerialBT.begin("RAFAB"); //Bluetooth device name; Serial.println("ESP32_LED_Control"); Serial.println("The device started, now you can pair it with bluetooth!"); //Config servo ledcSetup(PWMChannel, PWMFreq, PWMResolution); /* Attach the LED PWM Channel to the GPIO Pin */ ledcAttachPin(ServoPin, PWMChannel); ledcWrite(PWMChannel, dutyCycle); //Config leds // These lines are specifically to support the Adafruit Trinket 5V 16 MHz. // Any other board, you can remove this part (but no harm leaving it): #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) clock_prescale_set(clock_div_1); #endif // END of Trinket-specific code. pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) pixels.setBrightness(BRIGHTNESS); // Set BRIGHTNESS to about 1/5 (max = 255) pixels.clear(); // Set all pixel colors to 'off' i_red=255; //initial value to red for RGB i_blue=255;// initial value to blue for RGB i_green=255;// initial value to green for RGB //Set Light white when you turn on the lamp whiteLight();//Encendemos los leds con luz blanca b_autoServo=true;//Iniciamos con el modo auto del servomotor segun el sensor de luz //Waiting to connect to APP while(!SerialBT.connected(10000)) { Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); } hello();//Hello with leds when you connect your app } void loop() { if(b_autoServo){ autoServo(); } while (SerialBT.available()) { recv = SerialBT.readStringUntil('\n'); //recv = recv + string; delay(1); Serial.println(recv); } if (recv == "Y") { lightOn(); Serial.println("Turn on"); }else if (recv == "N") { pixels.clear(); delay(500); for(int i=0; iservoMax){value=servoMax;} if(valueservoMax){value=servoMax;} if(value